package org.sgx.gwteditors.client.impl1.simple;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.AbstractEditorError;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.EditorValueChangeEvent;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeDouble;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeInteger;
import org.sgx.gwteditors.client.impl1.ui.slider.HSliderWidget1;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.kiouri.sliderbar.client.event.BarValueChangedEvent;
import com.kiouri.sliderbar.client.event.BarValueChangedHandler;

public class InRangeDoubleEditor implements Editor<InRangeDouble>, BarValueChangedHandler {
	
	HSliderWidget1 widget;

	private InRangeDouble model;

//	private boolean editorNotify;
	
	private static final int SLIDER_MAXVALUE = 100; 
	private static final String SLIDER_WIDTH = "200px";
	
	public InRangeDoubleEditor() {
//		editorNotify=true;
	}
	@Override
	public boolean isSimple() {
		return true;
	}
	
	@Override
	public InRangeDouble flush() {
		double rv = getRealValue(widget.getValue());
		model.setValue(rv); 
		return model;
	}

	@Override
	public void load(InRangeDouble t) {
		this.model=t;
		widget=new HSliderWidget1(SLIDER_MAXVALUE, SLIDER_WIDTH);
		
//		/* IMPORTANT: we don't want to fire an event on load(), only when on user changes */ 
		setRealValue(t.getValue());
		widget.addBarValueChangedHandler(this);
	}

	@Override
	public HSliderWidget1 getWidget() {
		return widget;
	}

	@Override
	public void addValueChangeListener(ValueChangeListener<InRangeDouble> l) {
		EditorEventManager.<InRangeDouble>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<InRangeDouble> l) {
		EditorEventManager.<InRangeDouble>getInstance().removeValueChangeListener(this, l);
	}
	
	@Override
	public List<EditorError<InRangeDouble>> getErrors() {
		return EditorErrorManager.<InRangeDouble>getInstance().getErrors(this);
	}
	@Override
	public void onBarValueChanged(BarValueChangedEvent event) {
//		if(editorNotify) {
			EditorValueChangeEvent<InRangeDouble> ev_ = new EditorValueChangeEvent<InRangeDouble>(this, flush(), null);		
			EditorEventManager.<InRangeDouble>getInstance().fireValueChangeEvent(this, ev_);	
//		}
	}
	private double getRealValue(double value) {
		double rv = (value/100.0)*(model.getMax()-model.getMin())+model.getMin();
		return rv; 
	}
	private void setRealValue(double value) {
		double rv = (value/100.0)*(model.getMax()-model.getMin())-model.getMin(); 
		widget.setValue((int) rv); 
	}
	
	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_INRANGEDOUBLE}; 
	}
	
}
